package com.pet.platform.controller;

import com.pet.platform.common.PageResult;
import com.pet.platform.common.Result;
import com.pet.platform.dto.PetDTO;
import com.pet.platform.dto.PetHealthDTO;
import com.pet.platform.dto.PetQueryDTO;
import com.pet.platform.entity.Pet;
import com.pet.platform.service.PetHealthService;
import com.pet.platform.service.PetService;
import com.pet.platform.util.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.validation.Valid;
import java.util.List;

@RestController
@RequestMapping("/api/pets")
public class PetController {

    @Autowired
    private PetService petService;

    @Autowired
    private PetHealthService petHealthService;

    // 管理员接口 - 获取宠物列表（分页）
    @GetMapping("/admin/list")
    public Result<PageResult<PetDTO>> getAdminPetList(PetQueryDTO queryDTO) {
        return Result.success(petService.findPage(queryDTO));
    }

    // 管理员接口 - 新增宠物
    @PostMapping("/admin/create")
    public Result<Void> createPet(@Valid @RequestBody PetDTO petDTO) {
        petService.create(petDTO);
        return Result.success();
    }

    // 管理员接口 - 更新宠物信息
    @PutMapping("/admin/{id}")
    public Result<Void> updatePet(@PathVariable Long id, @Valid @RequestBody PetDTO petDTO) {
        petDTO.setId(id);
        petService.update(id, petDTO);
        return Result.success();
    }

    // 管理员接口 - 删除宠物
    @DeleteMapping("/admin/{id}")
    public Result<Void> deletePet(@PathVariable Long id) {
        petService.delete(id);
        return Result.success();
    }

    // 管理员接口 - 上传宠物图片
    @PostMapping("/admin/{id}/image")
    public Result<String> updatePetImage(@PathVariable Long id, @RequestParam("file") MultipartFile file) {
        return Result.success(petService.updateImage(id, file));
    }

    // 以下是普通用户接口
    @GetMapping("/{id}")
    public Result<Pet> findById(@PathVariable Long id) {
        return Result.success(petService.findById(id));
    }

    @GetMapping
    public Result<PageResult<PetDTO>> findPage(PetQueryDTO queryDTO) {
        return Result.success(petService.findPage(queryDTO));
    }

    @PostMapping("/{id}/adopt")
    public Result<Void> adopt(@PathVariable Long id) {
        petService.adopt(id);
        return Result.success();
    }

    @PostMapping("/{id}/cancel-adoption")
    public Result<Void> cancelAdoption(@PathVariable Long id) {
        petService.cancelAdoption(id);
        return Result.success();
    }

    @GetMapping("/owner/{ownerId}")
    public Result<List<PetDTO>> findByOwnerId(@PathVariable Long ownerId) {
        return Result.success(petService.findByOwnerId(ownerId));
    }

    @GetMapping("/my")
    public Result<List<PetDTO>> getMyPets() {
        return Result.success(petService.findByOwnerId(SecurityUtils.getCurrentUserId()));
    }

    @GetMapping("/{petId}/health-records")
    public Result<List<PetHealthDTO>> getHealthRecords(@PathVariable Long petId) {
        return Result.success(petHealthService.findByPetId(petId));
    }

    @PostMapping("/{petId}/health-records")
    public Result<Void> createHealthRecord(@PathVariable Long petId, @Valid @RequestBody PetHealthDTO healthDTO) {
        healthDTO.setPetId(petId);
        petHealthService.create(healthDTO);
        return Result.success();
    }

    @PutMapping("/{petId}/health-records/{recordId}")
    public Result<Void> updateHealthRecord(
            @PathVariable Long petId,
            @PathVariable Long recordId,
            @Valid @RequestBody PetHealthDTO healthDTO) {
        healthDTO.setPetId(petId);
        petHealthService.update(recordId, healthDTO);
        return Result.success();
    }

    @DeleteMapping("/{petId}/health-records/{recordId}")
    public Result<Void> deleteHealthRecord(@PathVariable Long petId, @PathVariable Long recordId) {
        petHealthService.delete(recordId);
        return Result.success();
    }
}